package com.plugins.activity;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import com.alibaba.fastjson.JSON;
import com.plugins.activity.widget.AlbumViewPager;
import com.plugins.activity.widget.PlayerSeekBar;
import com.plugins.pluginlib.IBasePluginActivity;
import com.plugins.pluginlib.broadcast.BroadMessage;
import com.plugins.test.R;

import java.lang.ref.WeakReference;

public class TestActivity extends IBasePluginActivity {

    public static Context context;

    private ImageView mBackAlbum, mPlayingmode, mControl, mNext, mPre, mPlaylist, mCmt, mFav, mDown, mMore, mNeedle;
    private TextView mTimePlayed, mDuration;
    private PlayerSeekBar mProgress;

    private ActionBar ab;
    private ObjectAnimator mNeedleAnim, mRotateAnim;
    private AnimatorSet mAnimatorSet;
    private AlbumViewPager mViewPager;
    private BitmapFactory.Options mNewOpts;
    private View mActiveView;
    private WeakReference<ObjectAnimator> animatorWeakReference;
    private WeakReference<View> mViewWeakReference = new WeakReference<View>(null);
    private boolean isFav = false;
    private boolean isNextOrPreSetPage = false; //判断viewpager由手动滑动 还是setcruuentitem换页
    private FrameLayout mAlbumLayout;
    private RelativeLayout mLrcViewContainer;
    private TextView mTryGetLrc;
    private LinearLayout mMusicTool;
    private SeekBar mVolumeSeek;
    private Handler mHandler;
    private Handler mPlayHandler;
    private static final int VIEWPAGER_SCROLL_TIME = 390;
    private static final int TIME_DELAY = 500;
    private static final int NEXT_MUSIC = 0;
    private static final int PRE_MUSIC = 1;
    private Bitmap mBitmap;
    private long lastAlbum;
    private boolean print = true;
    private TextView tvTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        context = that;
        tvTitle = findViewById(R.id.tv_title);
        mAlbumLayout =  findViewById(R.id.headerView);
        mLrcViewContainer = findViewById(R.id.lrcviewContainer);

        mTryGetLrc = findViewById(R.id.tragetlrc);
        mMusicTool = findViewById(R.id.music_tool);

        mBackAlbum = findViewById(R.id.albumArt);
        mPlayingmode = findViewById(R.id.playing_mode);
        mControl = findViewById(R.id.playing_play);
        mNext = findViewById(R.id.playing_next);
        mPre = findViewById(R.id.playing_pre);
        mPlaylist = findViewById(R.id.playing_playlist);
        mMore = findViewById(R.id.playing_more);
        mCmt = findViewById(R.id.playing_cmt);
        mFav = findViewById(R.id.playing_fav);
        mDown = findViewById(R.id.playing_down);
        mTimePlayed = findViewById(R.id.music_duration_played);
        mDuration = findViewById(R.id.music_duration);
        mProgress = findViewById(R.id.play_seek);
        mNeedle = findViewById(R.id.needle);
        mViewPager = findViewById(R.id.view_pager);

        mNeedleAnim = ObjectAnimator.ofFloat(mNeedle, "rotation", -25, 0);
        mNeedleAnim.setDuration(200);
        mNeedleAnim.setRepeatMode(ValueAnimator.RESTART);
        mNeedleAnim.setInterpolator(new LinearInterpolator());

        mVolumeSeek = findViewById(R.id.volume_seek);
        mProgress.setIndeterminate(false);
        mProgress.setProgress(1);
        mProgress.setMax(1000);

    }

    @Override
    public void fromHostMessage(BroadMessage message) {
        Log.d("fromHostMessage",JSON.toJSONString(message));
        String msg = "来自宿主程序传递过来的参数：";
        msg+="command:"+message.getCommand()+"\n";
        msg+="status:"+message.getStatus()+"\n";
        msg+="message:"+message.getMessage()+"\n";
        msg+="param:"+ JSON.toJSONString(message.getParam())+"\n";
        msg+="result:"+JSON.toJSONString(message.getResult())+"\n";
        msg+="data:"+JSON.toJSONString(message.getData())+"\n";
        Log.e("fromHostMessage",msg);
        if(tvTitle!=null&&message.getParam()!=null){
            tvTitle.setText("正在播放:"+ message.getParam("key"));
        }
    }
}